<?php
namespace App\Http\Controllers;

use App\Http\Services\HopeDrawServices;
use Illuminate\Http\Request;
use App\Lib\Response;
use App\Http\Model\Hint;
use App\Http\Services\LuckDrawServices;

//心心相印
class ApiHopeDrawController extends BaseController
{
    protected $luckDrawService;

    public function __construct(){
        $this->luckDrawService = new HopeDrawServices();
    }

    /**
     * 心心相印中奖记录列表 今日礼物榜单
     */
    public function awardListV2(Request $request)
    {
        $limit = (int) $request->input('page_size', 10);
        $page = (int) $request->input('page_index', 0);
        $luckId = (int) $request->get('luck_id', 0);
        $userId = $this->getLoginUserId();

        $data = $this->luckDrawService->awardListV2($page, $limit, $userId, $luckId);
        
        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     * 心心相印奖品列表(目前开启奖池的最高池奖品列表,非外显礼物)
     */
    public function poolAwardListV2(Request $request)
    {
        $luckId = (int) $request->get('luck_id', 0);

        $data = $this->luckDrawService->poolAwardListV2($luckId);

        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 滚动中奖记录
     *
     * @param Request $request
     * @return void
     */
    public function marquee(Request $request){
        $top = $request->get('top', 40);
        //不是数字，或者小于等于0 返回参数错误
        if (!is_numeric($top) || $top <= 0) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        //获取当前用户当前活动最新中奖记录
        $ranking = $this->luckDrawService->marquee($top);
        Response::returnResult($ranking['code'], $ranking['data'], $ranking['msg']);
    }

    /**
     * 抽奖
     */
    public function wishpoolsDraw(Request $request){
        //获取抽奖次数
        $num = (int) $request->get('draw_num', 0);
        //获取房间id
        $roomId = (int) $request->get('room_id', 0);

        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if($num < 1) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        // if(!in_array($userId, [10000, 10001])) {
        //     Response::returnResult(Hint::PARAM_ERROR, [], '10:30-11:30系统维护，请稍后');
        // }

        //锁
        $key = 'WISHPOOL:wishPoolDraw:'.$userId;
        $ifLock = redis_lock($key, 30);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, [], '抽奖中, 同一时间请勿重复请求');
        }

        $data = $this->luckDrawService->wishPoolDraw($userId, $num, $roomId);
       
        redis_unlock($key);
        

        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }
        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 发飘屏
     */
    public function wishWheelDrawFull(Request $request){
        $orderId = (int) $request->input('order_id', 0);
        if(empty($orderId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHPOOL:wishPoolDrawFull:'.$orderId, 60);
        if (!$ifLock){
            // Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->luckDrawService->wishLuckDrawFull($orderId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }
        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 活动规则
     */
    public function rule(){
        $data = $this->luckDrawService->getRule();
        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     * 兑换告白币
     */
    public function exchange(Request $request){
        $num = $request->get('num', 0);
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if($num < 1) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $res = $this->luckDrawService->exchange($num, $userId);

        Response::returnResult($res['code'],$res['data'],$res['msg']);
    }

    /**
     * 心心相印购买道具
     */
    public function hookBuy(Request $request){
        $num = $request->get('num', 0);
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        if($num < 1) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        $res = $this->luckDrawService->hookBuy($num, $userId);

        Response::returnResult($res['code'],$res['data'],$res['msg']);
    }

    /**
     * 获取资产信息
     */
    public function balance(Request $request){
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $res = $this->luckDrawService->balance($userId);
        Response::returnResult(Hint::SUCCESS, $res);
    }

    /**
     * 获取海洋世界资产
     */
    public function hookBalance(){
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $res = $this->luckDrawService->hookBalance($userId);
        Response::returnResult(Hint::SUCCESS, $res);
    }

    /**
     * 获取用户中奖记录
     */
    public function wishLuckAwardListByUserId(Request $request){
        $pageNum = $request->get('page_num', 0);// 页数
        $pageSize = $request->get('page_size', 20);// 条数
        $userId = $this->getLoginUserId();
        $ua = $request->userAgent();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }

        //ios特殊处理分页数据从0开始,前端传递的是1
        $userAgent = explode(';',$ua);
        if(isset($userAgent[2]) && $userAgent[2] == 'iPhone'){
            $pageNum = $pageNum -1;
        }
        $data = $this->luckDrawService->wishLuckAwardListByUserId($pageNum, $pageSize, $userId);

        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     * 获取单次抽奖价格
     */
    public function getPrice(){
        $luckId = 1000;
        $data = $this->luckDrawService->getPrice($luckId);
        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     * 自动兑换Ob提示
     * 
     */
    public function wishLuckPop(Request $request){
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $num = (int) $request->input("num");
        $roomId = (int) $request->input("room_id");
        if($num < 1) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->luckDrawService->wishLuckPop($userId, $num, $roomId);

        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }
        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 确认弹窗
     */
    public function wishLuckConfirm(Request $request){
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $data = $this->luckDrawService->wishLuckConfirm($userId);

        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }
        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    //投入礼物展示列表
    public function wishLuckExGift(){
        $data = $this->luckDrawService->wishLuckExGift();
        Response::returnResult(Hint::SUCCESS, $data['data']);
    }
}
